
import http, { baseHeaders, baseParams } from "@/utils/request";
import { UrlCouponUse, UrlCouponVerify, UrlGetCoupons } from "@/utils/apiUrl";
import { isEmptyString } from "@/utils/utils";
import { Toast } from "antd-mobile";

/**
 * 获取coupons
 */
export const getCouponList = (reqCallback) => {
    return async (dispatch) => {
        console.log("getCouponList: " + UrlGetCoupons);

        //组装参数
        const params = baseParams();

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlGetCoupons, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("获取coupons-结束：=======" + resCode)
        console.log(resObj)
        console.log("获取coupons-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}

/**
 * coupon verify
 */
export const couponVerify = (code, reqCallback) => {
    return async (dispatch) => {
        console.log("couponVerify: " + UrlCouponVerify);

        //组装参数
        const params = baseParams();
        params.code = code || "";

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlCouponVerify, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("coupon verify-结束：=======" + resCode)
        console.log(resObj)
        console.log("coupon verify-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}

/**
 * coupon use
 */
export const couponUse = (cashCouponId, reqCallback) => {
    return async (dispatch) => {
        console.log("couponUse: " + UrlCouponUse);

        //组装参数
        const params = baseParams();
        params.cashCouponId = cashCouponId || "";

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlCouponUse, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("coupon use-结束：=======" + resCode)
        console.log(resObj)
        console.log("coupon use-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}
